package org.appfuse.webapp.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.appfuse.Constants;
import org.appfuse.model.BasicInfo;
import org.appfuse.model.Dutyinfo;
import org.appfuse.model.User;
import org.appfuse.service.BasicInfoManager;
import org.appfuse.service.DutyinfoManager;
import org.appfuse.webapp.form.DutyinfoForm;

public final class DutyinfoAction extends BaseAction {

	public ActionForward cancel(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("list");
	}

	public ActionForward delete(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("Entering 'delete' method");
		}
		DutyinfoForm form = (DutyinfoForm) actionForm;

		User user = getCurrentUser(request);
		DutyinfoManager mgr = (DutyinfoManager) getBean(Constants.DUTYINFO_MANAGER);
		Integer selectedId = form.getSelectedId();
		String a00 = user.getRecordId();
		BasicInfoManager basicInfoMgr = (BasicInfoManager) getBean(Constants.BASICINFO_MANAGER);
		BasicInfo basicInfo = (BasicInfo) basicInfoMgr.findById(a00);

		Dutyinfo theObject = (Dutyinfo) convert(form);
		theObject.setId(selectedId);

		try {
			theObject.setBasicInfo(basicInfo);
			mgr.delete(theObject);
			updateBasicInfo(theObject, basicInfo, basicInfoMgr, false);
			HttpSession session = request.getSession();
			List list = (List) session.getAttribute(Constants.DUTYINFO_LIST);
			list.remove(theObject);

		} catch (Exception e) {

		}

		return mapping.findForward("list");
	}

	public ActionForward edit(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("Entering 'edit' method");
		}

		DutyinfoForm form = (DutyinfoForm) actionForm;

		User user = getCurrentUser(request);

		DutyinfoManager mgr = (DutyinfoManager) getBean(Constants.DUTYINFO_MANAGER);
		Integer selectedId = form.getSelectedId();
		String a00 = user.getRecordId();
		Dutyinfo theObject = mgr.findById(a00, selectedId);

		if (theObject != null) {
			form = (DutyinfoForm) convert(theObject);
			updateFormBean(mapping, request, form);
		}

		return mapping.findForward("edit");
	}

	public ActionForward save(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("Entering 'save' method");
		}

		ActionMessages errors = actionForm.validate(mapping, request);

		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			return mapping.findForward("add");
		}

		DutyinfoManager mgr = (DutyinfoManager) getBean(Constants.DUTYINFO_MANAGER);
		User user = getCurrentUser(request);
		String a00 = user.getRecordId();

		// Extract attributes and parameters we will need
		ActionMessages messages = new ActionMessages();
		DutyinfoForm form = (DutyinfoForm) actionForm;
		Dutyinfo theObject = (Dutyinfo) convert(form);

		BasicInfoManager basicInfoMgr = (BasicInfoManager) getBean(Constants.BASICINFO_MANAGER);
		BasicInfo basicInfo = (BasicInfo) basicInfoMgr.findById(a00);

		try {
			// if id is not null then udpate the record else add it.

			HttpSession session = request.getSession();
			List list = (List) session.getAttribute(Constants.DUTYINFO_LIST);
			if (form.getId() != null) {
				theObject.setBasicInfo(basicInfo);
				mgr.update(theObject);
				updateBasicInfo(theObject, basicInfo, basicInfoMgr, true);
				int index = list.indexOf(theObject);
				list.set(index, theObject);
			} else {
				theObject.setId(mgr.newId(a00));
				basicInfo.getDutyInfos().add(theObject);
				theObject.setBasicInfo(basicInfo);
				// mgr.save(cgjxx);
				//basicInfoMgr.update(basicInfo);
				updateBasicInfo(theObject, basicInfo, basicInfoMgr, true);
				form.setId(theObject.getId());
				list.add(theObject);
			}
			String zcjb = theObject.getZcjb();
			theObject.setZc((String) Constants.GB8561_MAP
					.get(theObject.getZc()));
			theObject.setHdzgtj((String) Constants.ZB24_MAP.get(theObject.getHdzgtj()));
			theObject.setZcjb((String) Constants.ZB904_MAP.get(zcjb));
		} catch (Exception e) {
			e.printStackTrace();
		}

		// add success messages
		messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
				"dutyinfo.saved"));
		// save messages in session to survive a redirect
		saveMessages(request.getSession(), messages);

		return mapping.findForward("list");
	}

	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("Entering 'save' method");
		}

		return mapping.findForward("edit");
	}

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("Entering 'list' method");
		}

		User user = getCurrentUser(request);
		HttpSession session = request.getSession();

		String a00 = user.getRecordId();
		if (!StringUtils.isEmpty(a00)) {
			DutyinfoManager mgr = (DutyinfoManager) getBean(Constants.DUTYINFO_MANAGER);
			List list = mgr.findByA00(a00);
			if (list == null) {
				list = new ArrayList(0);
			} else {
				Iterator it = list.iterator();
				while (it.hasNext()) {
					Dutyinfo theObject = (Dutyinfo) it.next();
					String zcjb = theObject.getZcjb();
					theObject.setZc((String) Constants.GB8561_MAP.get(theObject
							.getZc()));
					theObject.setHdzgtj((String) Constants.ZB24_MAP.get(theObject.getHdzgtj()));
					theObject.setZcjb((String) Constants.ZB904_MAP.get(zcjb));
				}
			}

			session.setAttribute(Constants.DUTYINFO_LIST, list);
		}

		return mapping.findForward("list");
	}

  
	protected void updateBasicInfo(Dutyinfo theObject, BasicInfo basicInfo,
			BasicInfoManager basicInfoMgr, boolean isAdd) {
		ArrayList<Dutyinfo> tmp = new ArrayList<Dutyinfo>(basicInfo
				.getDutyInfos());
		if (isAdd){
			tmp.add(theObject);
		}else{
			tmp.remove(theObject);
		}
		if (tmp.size() > 0){
		Dutyinfo dlt = Collections.max(tmp);
		
		if (StringUtils.isEmpty(basicInfo.getZc()) ||
				dlt.getZc().compareToIgnoreCase(basicInfo.getZc()) != 0) {
			basicInfo.setZc((String) Constants.GB8561_MAP
					.get(theObject.getZc()));
			basicInfo.setZcjb(dlt.getZcjb());
		}
		}else{
			basicInfo.setZc("");
			basicInfo.setZcjb("");
		}
		basicInfoMgr.update(basicInfo);
	}
}
